package com.buaa.divinationmaster.core.algorithm;

import com.buaa.divinationmaster.core.constants.DivinationType;

import java.util.HashMap;
import java.util.Map;

/**
 * 占卜输入参数类
 * 封装不同占卜算法所需的输入参数
 */
public class DivinationInput {
    
    private DivinationType type;
    private String question;
    private Map<String, Object> parameters;
    private long timestamp;
    
    // 构造函数
    public DivinationInput(DivinationType type) {
        this.type = type;
        this.parameters = new HashMap<>();
        this.timestamp = System.currentTimeMillis();
    }
    
    public DivinationInput(DivinationType type, String question) {
        this(type);
        this.question = question;
    }
    
    // 基础Getter和Setter
    public DivinationType getType() { return type; }
    public void setType(DivinationType type) { this.type = type; }
    
    public String getQuestion() { return question; }
    public void setQuestion(String question) { this.question = question; }
    
    public long getTimestamp() { return timestamp; }
    public void setTimestamp(long timestamp) { this.timestamp = timestamp; }
    
    // 参数管理方法
    public void addParameter(String key, Object value) {
        parameters.put(key, value);
    }
    
    public Object getParameter(String key) {
        return parameters.get(key);
    }
    
    public Object getParameter(String key, Object defaultValue) {
        return parameters.getOrDefault(key, defaultValue);
    }
    
    public boolean hasParameter(String key) {
        return parameters.containsKey(key);
    }
    
    public void removeParameter(String key) {
        parameters.remove(key);
    }
    
    public void clearParameters() {
        parameters.clear();
    }
    
    public Map<String, Object> getAllParameters() {
        return new HashMap<>(parameters);
    }
    
    // 类型安全的参数获取方法
    public String getStringParameter(String key) {
        Object value = parameters.get(key);
        return value instanceof String ? (String) value : null;
    }
    
    public String getStringParameter(String key, String defaultValue) {
        String value = getStringParameter(key);
        return value != null ? value : defaultValue;
    }
    
    public Integer getIntParameter(String key) {
        Object value = parameters.get(key);
        if (value instanceof Integer) {
            return (Integer) value;
        } else if (value instanceof Number) {
            return ((Number) value).intValue();
        }
        return null;
    }
    
    public Integer getIntParameter(String key, Integer defaultValue) {
        Integer value = getIntParameter(key);
        return value != null ? value : defaultValue;
    }
    
    public Boolean getBooleanParameter(String key) {
        Object value = parameters.get(key);
        return value instanceof Boolean ? (Boolean) value : null;
    }
    
    public Boolean getBooleanParameter(String key, Boolean defaultValue) {
        Boolean value = getBooleanParameter(key);
        return value != null ? value : defaultValue;
    }
    
    public Long getLongParameter(String key) {
        Object value = parameters.get(key);
        if (value instanceof Long) {
            return (Long) value;
        } else if (value instanceof Number) {
            return ((Number) value).longValue();
        }
        return null;
    }
    
    public Long getLongParameter(String key, Long defaultValue) {
        Long value = getLongParameter(key);
        return value != null ? value : defaultValue;
    }
    
    public Float getFloatParameter(String key) {
        Object value = parameters.get(key);
        if (value instanceof Float) {
            return (Float) value;
        } else if (value instanceof Number) {
            return ((Number) value).floatValue();
        }
        return null;
    }
    
    public Float getFloatParameter(String key, Float defaultValue) {
        Float value = getFloatParameter(key);
        return value != null ? value : defaultValue;
    }
    
    // 占卜特定参数的便捷方法
    
    /**
     * 设置用户交互种子（用于塔罗牌算法）
     */
    public void setUserInteractionSeed(long seed) {
        addParameter("userSeed", seed);
    }
    
    public Long getUserInteractionSeed() {
        return getLongParameter("userSeed");
    }
    
    /**
     * 设置抽牌数量（用于塔罗牌和雷诺曼）
     */
    public void setCardCount(int count) {
        addParameter("cardCount", count);
    }
    
    public Integer getCardCount() {
        return getIntParameter("cardCount");
    }
    
    /**
     * 设置牌阵类型（用于塔罗牌）
     */
    public void setSpreadType(String spreadType) {
        addParameter("spreadType", spreadType);
    }
    
    public String getSpreadType() {
        return getStringParameter("spreadType");
    }
    
    /**
     * 设置起卦方式（用于梅花易数）
     */
    public void setDivinationMethod(String method) {
        addParameter("divinationMethod", method);
    }
    
    public String getDivinationMethod() {
        return getStringParameter("divinationMethod");
    }
    
    /**
     * 设置起卦时间（用于梅花易数时间起卦）
     */
    public void setDivinationTime(long time) {
        addParameter("divinationTime", time);
    }
    
    public Long getDivinationTime() {
        return getLongParameter("divinationTime");
    }
    
    /**
     * 设置数字参数（用于梅花易数数字起卦）
     */
    public void setNumbers(int upperNumber, int lowerNumber, int movingLine) {
        addParameter("upperNumber", upperNumber);
        addParameter("lowerNumber", lowerNumber);
        addParameter("movingLine", movingLine);
    }
    
    public Integer getUpperNumber() {
        return getIntParameter("upperNumber");
    }
    
    public Integer getLowerNumber() {
        return getIntParameter("lowerNumber");
    }
    
    public Integer getMovingLine() {
        return getIntParameter("movingLine");
    }
    
    // 验证方法
    public boolean isValid() {
        return type != null && 
               question != null && 
               !question.trim().isEmpty() &&
               question.length() <= 500; // 限制问题长度
    }
    
    public String getValidationError() {
        if (type == null) {
            return "占卜类型不能为空";
        }
        if (question == null || question.trim().isEmpty()) {
            return "占卜问题不能为空";
        }
        if (question.length() > 500) {
            return "占卜问题不能超过500个字符";
        }
        return null;
    }
    
    @Override
    public String toString() {
        return "DivinationInput{" +
                "type=" + type +
                ", question='" + question + '\'' +
                ", parameters=" + parameters.size() + " items" +
                ", timestamp=" + timestamp +
                '}';
    }
}
